<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="author" content="lijinbo" />
    <title>213-中文字符串排序</title>
    <style>
      a {
        margin: 8px;
        display: block;
      }
    </style>
  </head>

  <body>
    <p>
      参考文章：
      <a
        href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare"
        target="_blank"
      >
        localeCompare
      </a>

      <a href="https://www.douyin.com/video/7179912051605703995" target="_blank">
        LocaleCompare带你轻松搞定编码顺序和字典顺序
      </a>
    </p>
    <script>
      const getList = () => ['上海', '北京', '杭州', '广东', '深圳', '西安', '2', '1', 'b', 'a']
      // 1. 直接使用 sort 默认以编码顺序排序
      const list1 = getList()
      list1.sort()
      console.log(list1, '-->>> list1') // ["1","2","a","b","上海","北京","广东","杭州","深圳","西安"]

      // 2. 使用 localeCompare 以字典顺序排序
      const list2 = getList()
      list2.sort((a, b) => a.localeCompare(b))
      console.log(list2, '-->>> list2') // ["1","2","北京","广东","杭州","上海","深圳","西安","a","b"]
    </script>
  </body>
</html>
